function toggleFolderForm(show = true) {
    const formDiv = document.getElementById('folder-form');
    const button = document.getElementById('create-btn');
    const input = document.getElementById('new-folder-name');
    formDiv.style.display = show ? 'block' : 'none';

    if (show) {
        form.style.display = 'flex';
        input.focus(); // 自动聚焦输入框
    } else {
        form.style.display = 'none';
        input.value = ''; // 清空
    }
}

function createFolder() {
    const knowbaseName = document.getElementById('new-folder-name').value.trim();

    if (!knowbaseName) {
        alert("请输入知识库名称");
        return;
    }

    fetch(KNOWBASE_CREATE_URI, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': '{{ csrf_token }}'
        },
        body: JSON.stringify({
            // path: currentPath,
            knowbase_name: knowbaseName
        })
    })
        .then(response => response.json())
        .then(data => {
            if (data.statusCode === 200) {
                alert(data.message);
                toggleFolderForm(false); // 隐藏表单
                location.reload();       // 刷新页面查看新文件夹
            } else {
                alert("创建失败：" + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert("发生错误：" + error);
        });
}

// 点击页面其他地方隐藏表单（可选增强体验）
// document.addEventListener('click', function(e) {
//     const form = document.getElementById('folder-form');
//     const button = document.getElementById('create-btn');
//     const headerBar = document.getElementById('header-bar');
//
//     if (!headerBar.contains(e.target) && e.target !== button && e.target !== form && !form.contains(e.target)) {
//         form.style.display = 'none';
//     }
// });